package ioStream.file;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:文件创建学习
 * User:22954
 * Date:2024-03-14
 * Time:20:25
 */
@SuppressWarnings("all")
public class CreatFile {
    /*
    new File(String pathname)  根据完整路径构建一个File对象
    new File(File parent, String child)  根据父目录文件 + 子路径 构建一个File对象
    new File(String parent, String child)  根据父目录 + 子路径 构建一个File对象
    creatNewFile() 创建新文件----文件已存在时不会重复创建或覆盖
     */
    @Test
    public void creat1(){//new File(String pathname)
        String pathname = "f:/Test/NewFile1.txt";
        File file = new File(pathname);
        try {
            file.createNewFile();
            System.out.println("File was successfully created !");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void creat2(){//new File(File parent, String child)
        String parentPath = "f:/Test";
        File parentFilePath = new File(parentPath);
        String child = "NewFile2.txt";
        File file = new File(parentFilePath, child);
        try {
            file.createNewFile();
            System.out.println("File was successfully created !");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void creat3(){//new File(String parent, String child)
        String parent = "f:/Test";
        String child = "NewFile3.txt";
        File file = new File(parent, child);
        try {
            file.createNewFile();
            System.out.println("File was successfully created !");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
